#include "submitorder.h"
#include "ui_submitorder.h"
#include<QDebug>
#include<QTextBrowser>
#include<QMessageBox>
SubmitOrder::SubmitOrder(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SubmitOrder)
{
    ui->setupUi(this);
    winCon=nullptr;
    connect(ui->pushButton,&QPushButton::clicked,this,&SubmitOrder::checkOut);
    connect(ui->tableWidget,&QTableWidget::cellClicked,this,&SubmitOrder::selectOrderRole);
    this->setWindowTitle("订单结算");
}

SubmitOrder::~SubmitOrder()
{
    delete ui;
}

void SubmitOrder::setWindowController(WindowController* winCon__)
{
    winCon=winCon__;

    //进行信息初始化
    //订单界面
     showOrder();
    //打折界面
    vector<OrderRoleBase*> roles=winCon->getOrderRoles();
    unsigned int rowSize=roles.size();
    ui->tableWidget->setRowCount(rowSize);
    for(unsigned int i=0;i<rowSize;i++)
    {
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::fromStdString(roles[i]->toString())));
        QTableWidgetItem *check = new QTableWidgetItem();
        check->setCheckState(Qt::Unchecked);
        ui->tableWidget->setItem(i,1,check);
    }
    showPrice();

}

void SubmitOrder::showPrice()
{
    ui->label_2->setText("订单价格为："+QString::number(winCon->getOrder().getPrice(),'f',2));
}

void SubmitOrder::showOrder()
{
     //ui->textBrowser->setMarkdown();
     ui->textBrowser->setMarkdown(QString::fromStdString(winCon->getOrder().toString()));

}

void SubmitOrder::checkOut()
{
    winCon->checkOut();
    winCon->clearOrder();
    emit checkOnOver();
    this->close();
}

void SubmitOrder::selectOrderRole(int row,int column)
{
     Q_UNUSED(column);
    winCon->delAllOrderRole();
   row=ui->tableWidget->rowCount();
   for(int i=0;i<row;i++)
       if(ui->tableWidget->item(i,1)->checkState())
       {
            winCon->addSelectOrderRole(winCon->getOrderRoles()[i]);
       }
   showPrice();
   showOrder();
}

void SubmitOrder::on_pushButton_clicked()
{
    QMessageBox::information(this,tr("success"),tr("提交完成"),QMessageBox::Yes);
}

